<template>
  <div v-if="visible" class="download-progress-overlay">
    <div class="download-progress-modal">
      <div class="progress-header">
        <h3>正在下载更新</h3>
        <p class="progress-subtitle">下载完成后将自动重启安装</p>
      </div>
      <div class="progress-content">
        <div class="progress-bar">
          <div class="progress-fill" :style="{ width: percent + '%' }"></div>
        </div>
        <div class="progress-info">
          <div class="percent">{{ percent }}%</div>
          <div class="speed">{{ speed }}</div>
        </div>
        <div class="progress-details">
          <span>{{ formatBytes(downloaded) }} / {{ formatBytes(total) }}</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from "vue";

// 响应式数据
const visible = ref(false);
const percent = ref(0);
const speed = ref("0 KB/s");
const downloaded = ref(0);
const total = ref(0);

// 格式化字节大小
const formatBytes = (bytes) => {
  if (bytes === 0) return "0 B";
  const k = 1024;
  const sizes = ["B", "KB", "MB", "GB"];
  const i = Math.floor(Math.log(bytes) / Math.log(k));
  return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + " " + sizes[i];
};

// 监听主进程的进度更新
onMounted(() => {
  if (window.electronAPI) {
    window.electronAPI.onShowDownloadProgress(() => {
      visible.value = true;
      percent.value = 0;
      speed.value = "0 KB/s";
      downloaded.value = 0;
      total.value = 0;
    });

    window.electronAPI.onUpdateDownloadProgress((data) => {
      percent.value = data.percent;
      speed.value = data.speed;
      downloaded.value = data.downloaded;
      total.value = data.total;
    });

    window.electronAPI.onHideDownloadProgress(() => {
      visible.value = false;
    });
  }
});
</script>

<style scoped>
.download-progress-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9999;
}

.download-progress-modal {
  background: white;
  border-radius: 8px;
  padding: 24px;
  min-width: 400px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
}

.progress-header {
  margin-bottom: 20px;
  text-align: center;
}

.progress-header h3 {
  margin: 0;
  color: #333;
  font-size: 18px;
  font-weight: 600;
}

.progress-subtitle {
  margin: 8px 0 0 0;
  color: #666;
  font-size: 14px;
  font-weight: 400;
}

.progress-content {
  text-align: center;
}

.progress-bar {
  width: 100%;
  height: 8px;
  background: #e0e0e0;
  border-radius: 4px;
  overflow: hidden;
  margin-bottom: 12px;
}

.progress-fill {
  height: 100%;
  background: linear-gradient(90deg, #4caf50, #45a049);
  border-radius: 4px;
  transition: width 0.3s ease;
}

.progress-info {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
}

.percent {
  font-size: 16px;
  font-weight: 600;
  color: #333;
}

.speed {
  font-size: 14px;
  color: #666;
}

.progress-details {
  font-size: 12px;
  color: #999;
}
</style>
